//
//  XLEmoticonShardTools.swift
//  XLVeibo
//
//  Created by macbook on 16/7/5.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit

//每行显示的个数
let emoticonMaxRow = 7
//每列显示的列数
let emiticonMaxCol = 3

//每页显示的个数

let emiticonMaxCount = emoticonMaxRow * emiticonMaxCol - 1

class XLEmoticonShardTools: NSObject {
    
    //全局访问点
    static let shardTools:XLEmoticonShardTools = XLEmoticonShardTools()
    
    //获取bundle路径
    lazy var emoticonBundle:NSBundle = {
    
        //获取
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        
        return NSBundle(path: path)!
    
    }()
    
    //default 表情一维数组
    lazy var defaultEmoticon:[XLEmoticon] = {
    
        return self.getEmotich("default/")
        
    }()
    
    //emoji表情一维数组
    lazy var emojiEmoticon:[XLEmoticon] = {
    
        return self.getEmotich("emoji/")
    }()
    
    //浪小花一维数组
    lazy var lxhEmoticon:[XLEmoticon] = {
    
        return self.getEmotich("lxh/")
    }()
    
    
    //表情三维数组
    lazy var allEmotion:[[[XLEmoticon]]] = {
    
        return[
        self.getEmoticonGroups(self.defaultEmoticon),
        self.getEmoticonGroups(self.emojiEmoticon),
         self.getEmoticonGroups(self.lxhEmoticon)
          ]
    }()
    
//    
//    func ioi(){
//    
//        self.getEmoticonGroups(self.defaultEmoticon)
//        self.getEmoticonGroups(self.emojiEmoticon)
//        self.getEmoticonGroups(self.lxhEmoticon)
//    }
    
    
   // /Users/macbook/Desktop/微博练习/VeiBoLianXi/XLVeibo/XLVeibo/Classes/Compose(中间)/Views/Emoticon/Emoticons.bundle/Contents/Resources/emoji/info.plist
    //读取plist文件plist中的数组转成模型  一维数组
    func getEmotich(fileName:String)->[XLEmoticon]{
    
        //路径
        let file = emoticonBundle.pathForResource("\(fileName)info.plist", ofType: nil)!
        
        //plist转数组
        let array = NSArray(contentsOfFile: file)!
        
        //创建可变数组，存储模型
        var arrM:[XLEmoticon] = [XLEmoticon]()
        
        for dic in array {
            
            //模型
            let model = XLEmoticon(dict: dic as![String:AnyObject])
            //获取路径
            model.path = "\(fileName)"
            //保存模型
            arrM.append(model)
            
        }
        return arrM
    
    }
    
    //一维数组转为二维数组
    func getEmoticonGroups(emoticon:[XLEmoticon])->[[XLEmoticon]]{
    
        //计算页数
        let page = (emoticon.count + emiticonMaxCount - 1) / emiticonMaxCount
        
        //创建一个可变数组
        var arrM:[[XLEmoticon]] = [[XLEmoticon]]()
        
        for i in 0..<page {
            
            //位置和长度
            let loc = emiticonMaxCount * i
            var len = emiticonMaxCount
            
            //防止默认表情越界
            if loc + len > emoticon.count {
                len = emoticon.count - loc
            }
            
            //截取范围
            let range = NSMakeRange(loc, len)
            //数组截取
            let arr = (emoticon as NSArray).subarrayWithRange(range) as![XLEmoticon]
            //保存一维数组
            arrM.append(arr)
        }
        
        //返回二维数组
        return arrM
    
    }
    
    func getSearchEmoticon(chs: String) -> XLEmoticon?{
        
        //遍历默认
        
        for e in defaultEmoticon {
            
            if e.chs == chs {
                
                return e
            }
        }
        
        //遍历浪小花
        for e in lxhEmoticon {
            if e.chs == chs {
                
                return e
            }
        }
        
        return nil
        
    }
}






